package com.sdwz.borrow.controller;


import com.sdwz.borrow.client.LibraryClient;
import com.sdwz.borrow.client.UserClient;
import com.sdwz.borrow.entity.AppointmentInfo;
import com.sdwz.borrow.entity.BookInfo;
import com.sdwz.borrow.entity.UserInfo;
import com.sdwz.borrow.entity.vo.AppointmentVo;
import com.sdwz.borrow.service.AppointmentInfoService;
import com.sdwz.commonutil.Result;
import com.sdwz.serviceutil.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 预约信息表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-03-30
 */
@RestController
@RequestMapping("/api/borrow")
public class AppointmentApiController {

    @Autowired
    AppointmentInfoService appointmentInfoService;

    @Autowired
    LibraryClient libraryClient;

    @Autowired
    UserClient userClient;

    /**
     * 添加预约
     * @param request
     * @param bookIds
     * @return
     */
    @PostMapping("/auth/addAppointment")
    public Result addAppointment(HttpServletRequest request, @RequestBody List<String> bookIds){
        Long userId = JwtUtil.getUserId(request.getHeader("token"));

        UserInfo user = userClient.getUser(String.valueOf(userId));
        if (user.getStatus() == -2){
            return Result.fail().message("借阅卡已锁定，请联系管理员");
        }
        if (user.getStatus() == 0){
            return Result.fail().message("请先到个人信息中实名认证，添加借阅卡");
        }

        List<Map<String, Object>> maps = appointmentInfoService.addAppointments(String.valueOf(userId), bookIds);

        return Result.ok(maps);
    }

    /**
     * 取消预约
     * @param appoId
     * @return
     */
    @DeleteMapping("/auth/cancelAppointment/{appoId}")
    public Result cancelAppointment(@PathVariable String appoId){
        AppointmentInfo appointmentInfo = appointmentInfoService.getById(appoId);
        //删除预约表信息
        boolean flag = appointmentInfoService.removeById(appoId);
        if (!flag){
            return Result.fail().message("取消失败");
        }

        //书籍可借阅数加一
        BookInfo bookInfo = new BookInfo();
        bookInfo.setId(appointmentInfo.getBookId());
        int loanableQuantity = libraryClient.getBook(bookInfo.getId()).getLoanableQuantity();
        bookInfo.setLoanableQuantity(loanableQuantity+1);
        flag = libraryClient.changeLoanableQuantity(bookInfo);
        if (!flag){
            return Result.fail().message("取消失败");
        }
        return Result.ok();
    }

    /**
     * 删除超时预约，仅删除预约表信息
     * @param appoId
     * @return
     */
    @DeleteMapping("/auth/delAppointment/{appoId}")
    public Result delAppointment(@PathVariable String appoId){

        //删除预约表信息
        boolean flag = appointmentInfoService.removeById(appoId);
        if (!flag){
            return Result.fail().message("取消失败");
        }
        return Result.ok();
    }

    /**
     * 查看个人预约信息
     * @param request
     * @return
     */
    @GetMapping("/auth/getAppoInfo")
    public Result getAppointmentInfo(HttpServletRequest request){
        String userId = String.valueOf(JwtUtil.getUserId(request.getHeader("token")));

        List<AppointmentVo> result = appointmentInfoService.getAppointmentInfoByUserId(userId);
        return Result.ok(result);
    }


}

